<?php


namespace app\api\controller\xilumarket;


use app\common\controller\XilumarketApi;
use app\common\model\xilumarket\Order;
use app\common\model\xilumarket\OrderAppeal;
use think\Db;
use think\Exception;
use function fast\array_get;

class Appeal extends XilumarketApi
{
    protected $noNeedRight = ['*'];


    /**
     * 售后列表
     */
    public function before_appeal(){
        $order_id = $this->request->param('order_id');
        $order = $order_id ? Order::with(['orderProduct'=>function($query){
            $query->field(['id','order_id','product_id','product_name','image','product_price','sum_price']);
        }])->field(['id','order_no','pay_price','total_price','total_price','freight_type','pay_status','freight_status','order_status','refund_status','receipt_status','comment_status'])->where('id',$order_id)->find() : null;
        if(!$order) $this->error("订单不存在");
        $appeal = OrderAppeal::where('order_id',$order_id)
            ->where('order_product_id',$order->order_product[0]['id'])
            ->find();
        if($appeal) $this->error("已有申诉或者申诉已处理");
        $this->success("",$order);
    }

    /**
     * 申请售后
     */
    public function appeal(){

        $params = $this->request->param('');
        Db::startTrans();
        try {
            $result = OrderAppeal::add($params,$this->auth->getUser());
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        Db::commit();
        $this->success('提交售后成功',$result);
    }

}